<?php
//include 'conn_mysql.php';

//连接数据库操作
$conn=mysql_connect("localhost","root","365gold");
mysql_select_db("lxtx",$conn);
mysql_query('set names utf8');

session_start();
//定量
$APPID   = 'wx9692a5d1ecfa16b4';
$MCHID   = '1319572501';
$KEY     = '12345678901234567890123456789012';
$APPSECRET = '3683b66fa5708cdf0802d29f7e2314e2';



$state=$_REQUEST['state'];
if(strlen($state)==0){
	$is_enter="1";
	//跳转到微信授权
	$huidiao_lianjie=urlencode('http://haining.365jinbi.com/lvxingtianxia/wx_enter.php');
	$urls="https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx9692a5d1ecfa16b4&redirect_uri=".$huidiao_lianjie."&response_type=code&scope=snsapi_userinfo&state=".$is_enter."#wechat_redirect";

	echo "<script language='javascript' type='text/javascript'>";  
	echo "window.location.href='$urls'";  
	echo "</script>"; 
}else{
	//经过安全域名验证 获取openid
	session_start();
	$code			=		$_REQUEST['code'];

	//通过code换取网页授权access_token
	$url="https://api.weixin.qq.com/sns/oauth2/access_token";
	$data['appid']=$APPID;
	$data['secret']=$APPSECRET;
	$data['code']=$code;
	$data['grant_type']="authorization_code";
	//定义返回值接收变量；
	$httpstr =http($url, $data, 'GET');
	$de_json=json_decode($httpstr,true);

	$open_id 	    = 	$de_json['openid'];
	//$access_token 	= 	$de_json['access_token'];
//	$refresh_token  =   $de_json['refresh_token'];
//	
//	//刷新access_token
//	$url="https://api.weixin.qq.com/sns/oauth2/refresh_token";
//	$data['appid']=$APPID;
//	$data['grant_type']="refresh_token";
//	$data['refresh_token']=$refresh_token;
//	//定义返回值接收变量；
//	$httpstr =http($url, $data, 'GET');
//	$de_json=json_decode($httpstr,true);
//
//	$access_token 	= 	$de_json['access_token'];
//	$refresh_token  =   $de_json['refresh_token'];
//
//	//利用access_token 获取用户信息
//	$url="https://api.weixin.qq.com/sns/userinfo";
//	$data['access_token']=$access_token;
//	$data['openid']=$open_id;
//	$data['lang']="zh_CN";
//	//定义返回值接收变量
//	$httpstr =http($url, $data, 'GET');
//	$de_json=json_decode($httpstr,true);
//
//	$headimgurl 	= 	$de_json['headimgurl'];//用户头像
//	$nickname       =   $de_json['nickname'];//用户昵称
	
	//用session 记录下用户的openid
	

	//将用户的openid，头像，昵称保存入数据库
	$sql="SELECT * FROM `ecs_users` WHERE  openid='$open_id'";
	$query = mysql_query($sql);
	
	
	if(mysql_num_rows($query)<1){
           //include_once 'conn_login.php';
		  // exit;
		  
	}else{
		//记录下此时的手机号’
		while($row = mysql_fetch_array($query)){
			$_SESSION['user_id']=$row['user_id'];
			$_SESSION['phone']=$row['mobile_phone'];
			$_SESSION['openid'] =$row['openid'];
            
            if($row['token'])
            {
	 			$expire = time() + 3600*24*60;
				setcookie('token',$row['token'],$expire);           	
            }


			break;
		}
	}
	$_SESSION['check_openid']=1;
	if($_SESSION['referer_url'])$referer_url=$_SESSION['referer_url'];else  $referer_url="lvyou_list.php";
	header("location:$referer_url");
	$_SESSION['referer_url']='';
	exit;
}

function http($url,  $params, $method = 'GET', $header = array(), $multi = false){
	//var_dump($params);
	//echo json_encode($params);
	$opts = array(
			CURLOPT_TIMEOUT        => 30,
			CURLOPT_RETURNTRANSFER => 1,
			CURLOPT_SSL_VERIFYPEER => false,
			CURLOPT_SSL_VERIFYHOST => false,
			CURLOPT_HTTPHEADER     => $header
	);
	/* 根据请求类型设置特定参数 */
	switch(strtoupper($method)){
		case 'GET':
			$opts[CURLOPT_URL] = $url . '?' . http_build_query($params);
			break;
		case 'POST':
			//判断是否传输文件
			$params = $multi ? $params : http_build_query($params);
			$opts[CURLOPT_URL] = $url;
			$opts[CURLOPT_POST] = 1;
			$opts[CURLOPT_POSTFIELDS] = $params;
			break;
		default:
			throw new Exception('不支持的请求方式！');
	}
	/* 初始化并执行curl请求 */
	$ch = curl_init();
	curl_setopt_array($ch, $opts);
	$data  = curl_exec($ch);
	$error = curl_error($ch);
	curl_close($ch);
	if($error) throw new Exception('请求发生错误：' . $error);
	return  $data;
}
?>